package cn.com.ctcloud.saas.core.geetest.demo.demo1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.ctcloud.saas.core.geetest.sdk.GeetestConfig;
import cn.com.ctcloud.saas.core.geetest.sdk.GeetestLib;
import cn.com.ctcloud.saas.core.utils.WebUtil;

/**
 * Title: ChinaCT SaaS
 * <p>
 * Description: 使用Get的方式返回challenge和capthca_id,此方式以实现前后端完全分离的开发模式
 * <p>
 * Copyright: Copyright (c) 2008
 * <p>
 * Company: Liaoning Open Data Communication Co.,Ltd.
 * 
 * @author lihui
 * @version 7.0.0
 */
@WebServlet(urlPatterns = "/gt/register1", description = "极限验证")
public class StartCaptchaServlet extends HttpServlet {

	private static final long serialVersionUID = -4290056891701697019L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetestId(), GeetestConfig.getGeetestKey(),
				GeetestConfig.isNewfailback());

		String resStr = "{}";

		//自定义参数,可选择添加
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("user_id", GeetestConfig.getUserId()); //网站用户id
		param.put("client_type", GeetestConfig.getClientType()); //web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
		param.put("ip_address", WebUtil.getIpAddr(request)); //传输用户请求验证时所携带的IP

		//进行验证预处理
		int gtServerStatus = gtSdk.preProcess(param);

		//将服务器状态设置到session中
		request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
		//将userid设置到session中
		request.getSession().setAttribute("userid", GeetestConfig.getUserId());

		resStr = gtSdk.getResponseStr();

		PrintWriter out = response.getWriter();
		out.println(resStr);

	}

}